﻿#region Copyright (c) 2008 plexBB Team

/*  Copyright (c) 2008 plexBB Team
 *  
 *  This file is free software: you may copy, redistribute and/or modify it  
 *  under the terms of the GNU General Public License as published by the  
 *  Free Software Foundation, either version 2 of the License, or (at your  
 *  option) any later version.  
 *  
 *  This file is distributed in the hope that it will be useful, but  
 *  WITHOUT ANY WARRANTY; without even the implied warranty of  
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
 *  General Public License for more details.  
 *  
 *  You should have received a copy of the GNU General Public License  
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */

#endregion

using System;
using System.Linq;
using PlexBB.Data;
using PlexBB.ModuleModel;

namespace PlexBB.Controls.Admin.UserManagement
{
    public class AcpUsersInfo : IModuleInfo
    {
        #region IModuleInfo Members

        public string LangName
        {
            get { return "ACP_USER_MANAGEMENT"; }
        }

        public ModuleMode[] SupportedModes
        {
            get
            {
                return new[]
                           {
                               new ModuleMode("overview", "ACP_MANAGE_USERS"),
                               new ModuleMode("feedback", "ACP_USER_FEEDBACK"),
                               new ModuleMode("profile", "ACP_USER_PROFILE"),
                               new ModuleMode("prefs", "ACP_USER_PREFS"),
                               new ModuleMode("avatar", "ACP_USER_AVATAR"),
                               new ModuleMode("rank", "ACP_USER_RANK"),
                               new ModuleMode("sig", "ACP_USER_SIG"),
                               new ModuleMode("groups", "ACP_USER_GROUPS"),
                               new ModuleMode("perm", "ACP_USER_PERM"),
                               new ModuleMode("attach", "ACP_USER_ATTACH")
                           };
            }
        }

        #endregion
    }

    public class AcpUsers : ForumControl
    {
        public User ManagedUser { get; private set; }

        protected AcpUsers()
        {
            Lang.Require("Acp/Users");
            Lang.Require("Ucp");
            Lang.Require("Posting");
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Type controlType;

            string uParam = PlexBBContext.RequestAction["u"];
            if (uParam == null)
            {
                controlType = typeof(AcpUserSelect);
            }
            else
            {
                int managedUserID;
                int.TryParse(uParam, out managedUserID);
                ManagedUser = Data.Users.SingleOrDefault(u => u.UserID == managedUserID);

                if (ManagedUser == null)
                {
                    // show that user does not exist
                    throw new NotImplementedException();
                }
                else
                {
                    // add header control first
                    Controls.Add(Create<AcpUserHeader>());

                    // select controlType by ModuleMode of current module
                    switch (CurrentModule.ModuleMode)
                    {
                        case "overview":
                            controlType = typeof(AcpUserOverview);
                            break;
                        case "feedback":
                            controlType = typeof(AcpUserFeedback);
                            break;
                        case "profile":
                            controlType = typeof(AcpUserProfile);
                            break;
                        default:
                            throw new NotSupportedException();
                    }
                }
            }

            Controls.Add(Create(controlType));
        }
    }
}